<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
    >
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="考试状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column prop="name" label="考试名称" />
      <el-table-column prop="createTime" label="考试时间">
        <template slot-scope="scope">
          <span>{{ scope.row.startDate }}至{{ scope.row.endDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" width="100" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.currentState.label }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="得分"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
            <!-- v-if="scope.row.examStatus == null || scope.row.examStatus == 0" -->
          <el-button
            size="mini"
            type="text"
            @click="handlePreview(scope.row)"
            >马上考试</el-button
          >
          <!-- <div v-if="scope.row.examStatus == 1">正在批阅</div> -->
          <!-- <div v-if="scope.row.examStatus == 2">{{ scope.row.score }}分</div> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- :type="2"  -->
    <addExamForm
      :id="id"
      :open="open"
      @close="open = false"
      @close2="open = false"
      @success="handleQuery"
    />
    <previewExam :type="2" :id="id" :open="openPreview" @close="openPreview = false" @endexam="handleQuery"/>
    <students :id="id" :open="openAudit" @close="openAudit = false" />
  </div>
</template>

<script>
import {
  getExamPage,
  addExam,
  getExam,
  delExam,
  putExam
} from "@/api/cms/exma";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import addExamForm from "./components/addExam.vue";
import previewExam from "./components/previewExam";
import students from "./components/students";

export default {
  name: "Exam",
  components: { addExamForm, previewExam, students },
  data() {
    return {
      statusList: [
        {
          dictValue: "0",
          dictLabel: "等待考试"
        },
        {
          dictValue: "1",
          dictLabel: "正在考试"
        },
        {
          dictValue: "2",
          dictLabel: "考试完成"
        }
      ],
      id: "",
      examRoleList: [
        {
          id: 1,
          roleName: "学员"
        },
        {
          id: 2,
          roleName: "审批人员"
        },
        {
          id: 3,
          roleName: "管理员"
        }
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      list: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAudit: false,
      openPreview: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageSize: 10,
        pageNum: 1,
        state: ""
      },
      // 表单参数
      form: {
        chooseScore: "",
        fillInTheBlankScore: "",
        totalScore: "",
        name: "",
        startDate: "",
        endDate: "",
        duration: 50,
        remark: ""
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级部门不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" }
        ],
        email: [
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getStateTimeRange() {
      const now = Date.now();
      if (this.queryParams.state == 0) {
        return {
          startDate: now + 5 * 60 * 1000
        };
      }
      if (this.queryParams.state == 1) {
        return {
          endDate: ""
        };
      }
      if (this.queryParams.state == 2) {
        return {
          startDate: now - 5 * 60 * 1000
        };
      }
    },
    formatDateStatus(row) {
      const now = Date.now();
      const start = new Date(row.startDate).getTime();
      const end = new Date(row.endDate).getTime();
      if (now < start) {
        return {
          type: 0,
          label: "等待考试"
        };
      } else if (now > end) {
        return {
          type: 2,
          label: "考试完成"
        };
      } else {
        return {
          type: 1,
          label: "正在考试"
        };
      }
    },
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      getExamPage(this.queryParams).then(response => {
        this.list = response.rows.map(item => {
          item.currentState = this.formatDateStatus(item);
          return item;
        });
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chooseScore: "",
        fillInTheBlankScore: "",
        totalScore: "",
        name: "",
        startDate: "",
        endDate: "",
        duration: 50,
        remark: ""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.id = "";
      this.open = true;
      this.title = "添加考试";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.id = row.id;
      this.open = true;
    },
    handleAudit(row) {
      this.id = row.id;
      this.openAudit = true;
    },
    handlePreview(row) {
      this.id = row.id;
      this.openPreview = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deptId != undefined) {
            putExam(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExam(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除名称为"' + row.deptName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delExam(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    }
  }
};
</script>
